package com.outsourcing.bottle.ui;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.outsourcing.bottle.R;
import com.outsourcing.bottle.adapter.TestCustomAdapter;
import com.outsourcing.bottle.util.Utility;
import com.outsourcing.bottle.widget.ElasticScrollView;
import com.outsourcing.bottle.widget.ElasticScrollView.OnRefreshListener;

public class JavaTestActivity extends Activity implements Callback {
	ListView listView = null;
	View myWorkPopview = null;
    ElasticScrollView elasticScrollView = null;
    
    Handler handler = null;
    TestCustomAdapter adapter = null;
    ArrayList<ArrayList<String>> arrayList = null;
	private LinearLayout layout;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_test);
        handler = new Handler(this);
//        LinearLayout layout = (LinearLayout)findViewById(R.id.layout);
        
        elasticScrollView = (ElasticScrollView)findViewById(R.id.flow_refresh_view);
        
//        listView = (ListView)findViewById(R.id.list_contact);
        
        layout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.list, null);
        
        
        listView = (ListView) layout.findViewById(R.id.list_view);
        arrayList = new ArrayList<ArrayList<String>>();
        for(int i = 0;i<10;i++){
        	ArrayList<String> arrList = new ArrayList<String>();
        	for (int j=0;j<5;j++) {
            		arrList.add("子item"+j);
			}
        	arrayList.add(arrList);
        }
        TextView textView = new TextView(this);
        textView.setText("header");
        listView.addHeaderView(textView);
        
        adapter = new TestCustomAdapter(this, arrayList);
        listView.setAdapter(adapter);
        Utility.setListViewHeightBasedOnChildren(listView);

//		elasticScrollView.addChild(listView);
//		
		elasticScrollView.setonRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				Thread thread = new Thread(new Runnable() {

					@Override
					public void run() {
						try {
							Thread.sleep(3000);
							Message msg = handler.obtainMessage(0);
							handler.sendMessage(msg);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				});
				thread.start();
			}
		});
		elasticScrollView.addChild(layout);
		
    }
    
   int position; 
    
   public void refreshItem(int position){
	   this.position = position;
//	   arrayList.remove(position);
	   arrayList.get(position).add("新增");
	   adapter.notifyDataSetChanged();
	   
   }

	@Override
	public boolean handleMessage(Message msg) {
		// TODO Auto-generated method stub
		switch (msg.what) {
		case 0:
			elasticScrollView.onRefreshComplete();
			break;

		default:
			break;
		}
		return false;
	}
    
}